package Envo 
{
	import adobe.utils.ProductManager;
	import Envo.Assets.PlayerFloor;
	import Envo.Assets.SolidFloor;
	import flash.display.MovieClip;
	import com.as3toolkit.events.KeyboarderEvent;
	import com.as3toolkit.ui.Keyboarder;
	import flash.geom.Point;
	
	/**
	 * @author Sam Morrison
	 */
	public class Level extends MovieClip 
	{
		// Xml and game variables
		private var xml:XML;													// XML 
		private var game:Game;													// Game
		
		// Current level
		private var currentLevel:int;											// Current level
		
		// Level Array
		private var solidFloors:Array = new Array();							// Blocks in level
		public function get SolidFloors():Array { return solidFloors; }
		public function set SolidFloors(value:Array) { solidFloors = value; }
		
		// Viewing Area
		private const TOP_LIMIT:int = -50;
		private const BOTTOM_LIMIT:int = 480;
		private const RIGHT_LIMIT:int = 750;
		private const LEFT_LIMIT:int = -80;
		
		private var removedArray:Array = new Array();
		
		public function Level(xml:XML, game:Game) 
		{
			// Save xml and game variables and set current level to 0
			this.xml = xml;
			this.game = game;
			currentLevel = 0;
		}
		
		public function update(player:Player):void
		{
			// Create a temp point to hold the global locations for each block
			var tempPoint:Point;
			
			// Removes blocks if they are too far off the screen
			for (var i:int = 0; i < solidFloors.length; i++)
			{
				tempPoint = localToGlobal(new Point(solidFloors[i].x, solidFloors[i].y));
				
				if ((tempPoint.y > BOTTOM_LIMIT || tempPoint.x < LEFT_LIMIT)
					|| (tempPoint.y > BOTTOM_LIMIT || tempPoint.x > RIGHT_LIMIT)
					|| (tempPoint.y < TOP_LIMIT || tempPoint.x < LEFT_LIMIT)
					|| (tempPoint.y < TOP_LIMIT || tempPoint.x > RIGHT_LIMIT))
				{
					removedArray.push(solidFloors[i]);
					this.removeChild(solidFloors[i]);
					solidFloors.splice(i, 1);
					i--;
				}
				
			}
			
			// Adds blocks if they are on the screen
			for (var j:int = 0; j < removedArray.length; j++)
			{
				tempPoint = localToGlobal(new Point(removedArray[j].x, removedArray[j].y));
				
				if ((tempPoint.y < BOTTOM_LIMIT && tempPoint.x > LEFT_LIMIT)
					&& (tempPoint.y < BOTTOM_LIMIT && tempPoint.x < RIGHT_LIMIT)
					&& (tempPoint.y > TOP_LIMIT && tempPoint.x > LEFT_LIMIT)
					&& (tempPoint.y > TOP_LIMIT && tempPoint.x < RIGHT_LIMIT))
				{
					var tempFloor:SolidFloor = removedArray[j];
					addChild(tempFloor);
					removedArray.splice(j, 1);
					j--;
					solidFloors.push(tempFloor);
				}
			}
			
			
			// Move level
			this.x += player.previousX - player.x;
			this.y += player.previousY - player.y;
			
			// Move player along with level
			player.x += player.previousX - player.x;
			player.y += player.previousY - player.y;
		}
		
		public function load():void
		{
			// Read in level
			if (currentLevel < (xml.map as XMLList).length())
			{
				for (var i:int = 0; i < (xml.map[currentLevel].row as XMLList).length(); i++)
				{
					// Parse the string into an array of characters
					var array:Array = xml.map[currentLevel].row[i].split("");
					
					// Loop through elements in the parsed array
					for (var j:int = 0; j < array.length; j++)
					{
						if (array[j] as String == 's')
						{
							// Make a new block
							var solidFloor:SolidFloor = new SolidFloor();
							
							// Set location of block
							solidFloor.x = j * 25;
							solidFloor.y = i * 25;
							
							// Push onto floor layer and floor array to 
							// check collisions and add to level to display block
							solidFloors.push(solidFloor);
							addChild(solidFloor);
						}
						
						if (array[j] as String == 'p')
						{
							game.player.x = j * 25;
							game.player.y = i * 25;
						}
					}
				}
			}
			// Increase current level
			currentLevel++;
		}
	}
}